/**
 * 
 */
package com.ffcs.crmd.tsp.server.netty.processor;

import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ffcs.crmd.tsp.api.dto.job.TspJobDetailShell;
import com.ffcs.crmd.tsp.api.dto.netty.RemotingCommand;
import com.ffcs.crmd.tsp.api.dto.task.TspJobExecutionException;
import com.ffcs.crmd.tsp.api.dto.task.TspJobInstance;
import com.ffcs.crmd.tsp.api.netty.INettyProcessor;
import com.ffcs.crmd.tsp.common.constant.LoggerName;
import com.ffcs.crmd.tsp.core.util.XmlUtil;
import com.ffcs.crmd.tsp.server.application.ApplicationManagerHolder;
import com.ffcs.crmd.tsp.server.core.DataBaseHandler;
import com.ffcs.crmd.tsp.server.core.RemotingHandler;
import com.ffcs.crmd.tsp.server.manager.JobProcessManager;

import io.netty.channel.ChannelHandlerContext;

/**
 * 功能说明:处理完成的任务
 *
 * @author ZHONGFUHUA
 * 
 * @since 2.0.0-SNAPSHOT
 *
 */
public class CompleteJobDetailProcessor implements INettyProcessor {
    
    /**
     * 日志
     */
    private final static Logger LOG               = LoggerFactory.getLogger(LoggerName.ServerLoggerName);
    
    /**
     * 子任务缓存处理类
     */
    private JobProcessManager   jobProcessManager = ApplicationManagerHolder.getBean("jobProcessManager", JobProcessManager.class);
    
    /**
     * 功能说明:
     * 
     * @param remotingController
     */
    public CompleteJobDetailProcessor(RemotingHandler remotingHandler, DataBaseHandler dataBaseHandler) {
        // TODO Auto-generated constructor stub
    }
    
    @Override
    public RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand request) {
        TspJobDetailShell tspJobDetailShell = XmlUtil.fromXml(new String(request.getBody()), TspJobDetailShell.class);
        TspJobInstance tspJobDetail = tspJobDetailShell.getContext().getTspJobInstance();
        String jobName = tspJobDetail.getJobName();
        String jobDetailName = tspJobDetail.getName();
        TspJobExecutionException tspJobExecutionException = tspJobDetailShell.getTspJobExecutionException();
        if (tspJobDetailShell.isFinish()) {
            LOG.debug("SERVER-NETTY: recive a complete shutdown jobdetail request that jobKey=[{}], id=[{}], opaque=[{}].",
                tspJobDetail.getJobKey(), tspJobDetailShell.getId(), request.getOpaque());
        } else if (tspJobExecutionException == null) {
            LOG.debug("SERVER-NETTY: recive a complete jobdetail request that jobKey=[{}], id=[{}], opaque=[{}].", tspJobDetail.getJobKey(),
                tspJobDetailShell.getId(), request.getOpaque());
            jobProcessManager.completeJobDetail(jobName, ctx.channel(), jobDetailName, tspJobDetailShell.getId());
        } else {
            LOG.warn("SERVER-NETTY: receive a complete exception request that jobKey=[{}], id=[{}], opaque=[{}], refire=[{}].",
                tspJobDetail.getJobKey(), tspJobDetailShell.getId(), request.getOpaque(), tspJobExecutionException.isRefireJob());
            JobExecutionException jee = new JobExecutionException(tspJobExecutionException);
            if (tspJobExecutionException.isRefireJob()) {
                jee.setRefireImmediately(true);
            }
            jobProcessManager.completeJobDetail(jobName, ctx.channel(), jobDetailName, tspJobDetailShell.getId(), jee);
        }
        return null;
    }
}
